package labuladong.滑动窗口;

import java.util.HashMap;
import java.util.Map;

public class 最小覆盖子串 {
    public static void main(String[] args) {
        String s = "EBBANCF";
        String t = "ABC";
        System.out.println(minWindow(s, t));
    }

    public static String minWindow(String s, String t) {
        Map<Character, Integer> need = new HashMap<>();
        Map<Character, Integer> window = new HashMap<>();
        for (char c : t.toCharArray()) {
            need.put(c, need.getOrDefault(c, 0)+1);
        }
        int left = 0, right = 0;
        int valid = 0;
        int start = 0, len = Integer.MAX_VALUE;
        while (right < s.length()) {
            char c1 = s.charAt(right);
            right++;
            if (need.containsKey(c1)) {
                window.put(c1, window.getOrDefault(c1, 0)+1);
                if (window.get(c1) == need.get(c1)) {
                    valid++;
                }
            }

            while (valid == need.size()) {
                if (right - left < len) {
                    start = left;
                    len = right - left;
                }
                char c2 = s.charAt(left);
                left++;
                if (need.containsKey(c2)) {
                    if (window.get(c2) == need.get(c2)) {
                        valid--;
                    }
                    window.put(c2, window.get(c2)-1);
                    if(window.get(c2) <= 0) window.remove(c2);
                }
            }
        }
        return len == Integer.MAX_VALUE ? "" : s.substring(start, start+len);
    }
}
